//导入express
var express = require('express');
//form表单需要的中间件。
//实例化对象
var app = express();
//导入mongoose
var mongoose = require('mongoose');
//设置跨域访问
app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type");
    res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1');
	res.header("Content-Type", "application/json;charset=utf-8");
    return next();
});

mongoose.Promise = global.Promise 
//导入body-parser用来处理post过来的数据
var bodyParser = require('body-parser')
app.use('/public',express.static(__dirname+'/public'))
//使用中间件bodyparser
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

//通过地址寻找
app.use('/api',require('./route/api'));
//链接数据库
mongoose.connect('mongodb://localhost:27019/myblog',{useMongoClient: true},function(err){
	if(err){
		console.log('数据库连接失败')
	}else{
		console.log('数据库连接成功')
		app.listen(3006)
	}
})
